package com.me.darkness_is_coming.managers;

import java.io.*;

import com.badlogic.gdx.Gdx;

public class Save
{
	public static GameData game_data;
	
	public static void save()
	{
		try
		{
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data/data/com.me.darkness_is_coming/highscore.sav"));
			out.writeObject(game_data);
			out.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			Gdx.app.exit();
		}
	}
	
	public static void load()
	{
		try
		{
			if(!saveFileExists())
			{
                init();
				return;
			}
			ObjectInputStream in = new ObjectInputStream(new FileInputStream("data/data/com.me.darkness_is_coming/highscore.sav"));
            game_data = (GameData) in.readObject();
			in.close();
		}
		catch(Exception e)
        {
			e.printStackTrace();
			Gdx.app.exit();
		}
	}

    public static boolean saveFileExists()
    {
        File file = new File("data/data/com.me.darkness_is_coming/highscore.sav");
        boolean flag = file.exists();
        if (!flag)
        {
            try
            {
                file.createNewFile();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Gdx.app.exit();
            }
        }
        return flag;
    }
	
	public static void init()
	{
        game_data = new GameData();
        game_data.init();
		save();
	}
}
